package sourceagile.server.doclet.featureFile;

import java.util.ArrayList;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

public class TokenizeFeatureFile {

	public static String FEATURE_NAME = "featureName";
	public static String FEATURE_DESCRIPTION = "featureDescription";
	public static String FEATURE_PRIORITY = "priority";
	public static String FEATURE_ESTIMATE = "estimate";
	public static String FEATURE_STATUS = "status";

	public static JSONObject getJson(String featureSource) {

		ArrayList<String> tokensArray = new ArrayList<String>();

		String[] linesArray = featureSource.split(FeatureFileTokens.LINE_END);

		String featureName = null;

		String featureDescription = "";

		String priority = null;

		String estimate = null;

		String status = null;

		int i = 0;

		while (i < linesArray.length) {

			if (featureName == null) {

				String[] featureNameSplit = linesArray[i]
						.split(FeatureFileTokens.FEATURE_NAME_COMMAND);

				if (featureNameSplit.length > 0) {

					featureName = getTokensLine(tokensArray, featureNameSplit,
							FeatureFileTokens.FEATURE_NAME_COMMAND);

					i++;

					while (i < linesArray.length
							&& linesArray[i].trim().equals("")) {
						i++;
					}

					while (i < linesArray.length
							&& linesArray[i]
									.startsWith(FeatureFileTokens.COMMENT)) {

						String[] featurePriority = linesArray[i]
								.split(FeatureFileTokens.PRIORITY_COMMAND);

						if (featurePriority.length > 1) {

							priority = getTokensLine(tokensArray,
									featurePriority,
									FeatureFileTokens.PRIORITY_COMMAND);
						} else {

							String[] featureEstimate = linesArray[i]
									.split(FeatureFileTokens.ESTIMATE_COMMAND);

							if (featureEstimate.length > 1) {

								estimate = getTokensLine(tokensArray,
										featureEstimate,
										FeatureFileTokens.ESTIMATE_COMMAND);
							} else {

								String[] featureStatus = linesArray[i]
										.split(FeatureFileTokens.STATUS_COMMAND);

								if (featureStatus.length > 1) {

									status = getTokensLine(tokensArray,
											featureStatus,
											FeatureFileTokens.STATUS_COMMAND);

								} else {

									featureDescription += linesArray[i]
											.substring(1).trim();
								}
							}
						}

						i++;

						while (i < linesArray.length
								&& linesArray[i].trim().equals("")) {
							i++;
						}
					}

				} else {

					tokensArray.add(linesArray[i]);

					i++;
				}

			} else {

				tokensArray.add(linesArray[i]);

				i++;
			}

		}

		return loadFeature(featureName, featureDescription, priority, estimate,
				status);
	}

	private static String getTokensLine(ArrayList<String> tokensArray,
			String[] lineSplit, String token) {

		String tokenValue = null;

		tokensArray.add(lineSplit[0]);

		tokensArray.add(token);

		tokenValue = lineSplit[1];

		tokensArray.add(lineSplit[1]);

		return tokenValue.trim();
	}

	public static JSONObject loadFeature(String featureName,
			String featureDescription, String priority, String estimate,
			String status) {

		JSONObject jsonFeature = new JSONObject();

		try {

			jsonFeature.put(FEATURE_NAME, featureName);

			jsonFeature.put(FEATURE_DESCRIPTION, featureDescription);

			jsonFeature.put(FEATURE_PRIORITY, priority);

			jsonFeature.put(FEATURE_ESTIMATE, estimate);

			jsonFeature.put(FEATURE_STATUS, status);

		} catch (JSONException e) {

			e.printStackTrace();
		}

		return jsonFeature;
	}

}
